<template>
  <rx-dialog @handOk="handleSubmit" @cancel="cancel" :key="updateKey">
    <a-form :form="form" style="padding-right: 12px" class="pwslist">
      <a-row>
        <a-col :span="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.oldPassword','旧密码')" v-if="individual">
              <a-input-password  :placeholder="i18n('form.oldPassword','旧密码','input')"  v-decorator="['oldPwd', {rules: [{required: true, message: i18n('form.oldPassword','旧密码','input')}]}]"/>
            </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.newPassword','新密码')">
            <a-input-password  :placeholder="i18n('form.newPassword','新密码','input')"  v-decorator="['newPwd', {rules: [{required: true, message: i18n('form.newPassword','新密码','input')}]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="i18n('form.confirmNewPwd','确认新密码')">
            <a-input-password  :placeholder="i18n('form.againConfirm','再次输入新密码')"  v-decorator="['confirmPwd', {rules: [{required: true, message: i18n('form.confirmNewPwd','确认新密码','input')}]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </rx-dialog>
</template>

<script>
  import {BaseForm, RxDialog, Dialog, Util} from "@lowcode/jpaas-common-lib"
  import OsUserApi from '@/api/user/org/osUser'
  import LoginApi from '@/api/login'
  import {sm2} from 'sm-crypto'

  export default {
    name: "OsUserUpdatePwd",
    props: {
      userId: String,
      individual: {
        type: Boolean,
        default: false
      },
    },
    components: {
      RxDialog
    },
      data(){
        return {
            updateKey: "",
            //密码模式
            passwordmode: "default",
            //国产sm2公钥
            sw2PublicKey: process.env.VUE_APP_SM2_PUBLIC_KEY,
        }
      },
    mixins: [BaseForm],
    created () {
        this.getPasswordmode();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"osUser",key);
        },
        getTime(){
            return new Date().getTime();
        },
        //获取密码加密模式的配置参数
        getPasswordmode() {
            let self = this;
            LoginApi.getLoginConfig().then((res) => {
                let result = res.data;
                //密码加密模式
                self.passwordmode = result.passwordmode;
            });
        },
        sm2Encrypt(password) {
            const sm2 = require('sm-crypto').sm2
            let pwd= sm2.doEncrypt(password, this.sw2PublicKey);
            return pwd.toString();
        },
      save(values) {
            debugger;
        var self=this;
        //是否为个人修改密码
        if(this.individual){
            var parameter={oldPwd:values.oldPwd,newPwd:values.newPwd};
            if(this.passwordmode=="sm2"){
                parameter.isSm2Password ="YES";
                parameter.oldPwd=this.sm2Encrypt(parameter.oldPwd);
                parameter.newPwd=this.sm2Encrypt(parameter.newPwd);
            }
          OsUserApi.updatePassword(parameter).then(res=>{
            if(res.success){
              Util.closeWindow(self, "ok");
            }else{
                self.updateKey = self.getTime();
            }
          });
        } else {
          //管理员修改密码用户
          var parameter={userId:self.userId ,newPwd:values.newPwd};
            if(this.passwordmode=="sm2"){
                parameter.isSm2Password ="YES";
                parameter.newPwd=this.sm2Encrypt(parameter.newPwd);
            }
          OsUserApi.setPassword(parameter).then(res=>{
            if(res.success){
              Util.closeWindow(self, "ok");
            }else{
                self.updateKey = self.getTime();
            }
          });
        }
      },
      validForm(values){
        if(values.newPwd!=values.confirmPwd){
          this.$message.error(this.i18n('inconsistent','两次输入的新密码不一致!'));
          return false;
        }
        return true;
      },
    }
  }
</script>

<style scoped>
.pwslist{
  width: 90%;
  overflow: hidden;
  margin: auto;
  margin-top: 40px;
}
</style>